import pandas as pd
import random
import csv
import io
import sys

#sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='gb18030') 

alpha = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]
num = [1,2,3,4,5,6,7,8,9,0]
boolean = ["Y","N"]
rela = ["Positive","Negative"]
star = [1,2,3,4,5] 
filedata = pd.read_csv("Countries_Info.csv")
countries = filedata["Countries"]
rela = filedata["attitude"]

#D
def Calculate_D (relationship,sustainable,environmental_friendly,danger):
    D = 0
    if relationship == "Positive":
        D += 1
    if sustainable == "Y":
        D += 1
    if environmental_friendly == "Y":
        D += 1
    if danger == "N":   
        D += 1                   
    return D

#F
def Calculate_F (necessity,improve_quality,ben_to_natDev,invo_tech):
    F = 0
    if necessity == "Y":
        F += 1
    if improve_quality == "Y":
        F += 1
    if ben_to_natDev == "Y":
        F += 1
    if invo_tech == "Y":
        F += 1
    return F

#C
def Calculate_C (st_level):
    C = 0
    if st_level == 1:
        C += 1
    elif st_level == 2:
        C += 2
    elif st_level == 3:
        C += 3
    elif st_level == 4:
        C += 4
    elif st_level == 5:
        C += 5
    return C

#m:raw material
#c:country
#r:relationship between the country and China
#s:whether sustainable
#e:whether environmental friendly
#d:whether dangerous
#p:proportion
#st:star level
#n:necessity
#q:whether improve quality
#nd:whether beneficial to national development 
#istc:Whether involve scientific and technological component
#scores:DS,FS,CS

def WF():
    a = random.choice(alpha)
    b = str(random.choice(num))
    m = a+b        
    c = random.choice(countries)
    r = random.choice(rela)   
    s = random.choice(boolean)
    e = random.choice(boolean)
    d = random.choice(boolean)
    p = round(random.uniform(0.0005,0.1),4)
    st = random.choice(star)
    n = random.choice(boolean)
    q = random.choice(boolean)
    nd = random.choice(boolean)
    istc = random.choice(boolean)
    DS = Calculate_D(r,s,e,d)
    FS = Calculate_F(n,q,nd,istc)
    CS = Calculate_C(st)
    #DFC = f+c-d
    DFC = FS + CS - DS
    p = str(p)
    st = str(st)
    DS,FS,CS,DFC = str(DS),str(FS),str(CS),str(DFC)
    Temp = [m,c,r,s,e,d,p,st,n,q,nd,istc,DS,FS,CS,DFC]
    
    return Temp


#Info_Of_RawMaterials
space = [" "," "," "," "," "," "," "," "," "," "," "," "," "," "," "," "]
with open("Info_Of_RawMaterials.csv",'w',newline="",errors= "ignore") as f:
    f_csv = csv.writer(f)
    f_csv.writerow(["Raw_Material","Country","Relationship","Sustainable","Environmental_Friendly",
                    "Dangerous","Proportion","Star_level","Necessity","Improve_lifeQuality","Help_country_development",
                    "Involve_technology","D_score","F_score","C_score","DFC_score"])
    
    for i in range(180):
        N = random.randint(2,15)        
        for j in range(N):            
            info = WF()            
            f_csv.writerow(info)
        f_csv.writerow(space)
    
        
f.close()

























